<?php
Class ReturnsubAction extends Action{
	public function index(){
		import('ORG.Util.EditPage');
		$getLastMonth = $this->getLastMonth();
		$getLastMonthDate = $this->getLastMonthDate();
		$getThisMonth = $this->getThisMonth();
		$sql = "SELECT a.order_num,a.item_id,a.points,a.fee,a.refund_id,b.name,c.name AS ename FROM box_user_order_detail a LEFT JOIN box_user b on a.openid = b.openid LEFT JOIN box_user c ON a.item_id = c.id WHERE (a.substime>='{$getLastMonth['substime']}' AND a.subetime>='{$getLastMonth['subetime']}') AND a.order_num IN (SELECT order_num FROM box_user_order WHERE paytype=2 AND status=2) AND a.item_id NOT IN (SELECT user_id FROM box_expert_pub_record WHERE pubnums=4 AND pubdate BETWEEN  '{$getLastMonthDate['substime']}' AND  '{$getLastMonthDate['subetime']}') AND a.returntime NOT BETWEEN '{$getThisMonth['substime']}' AND '{$getThisMonth['subetime']}';";//item_id 为医生的ID
		//echo $sql;
		$expert = M()->query($sql);
		$Page = new Page(count($expert),10);
		$Page->setConfig('theme',"<ul class='pagination'><li><a>%totalRow% %header% %nowPage%/%totalPage% 页</a></li><li>%upPage%</li><li>%first%</li><li>%prePage%</li><li>%linkPage%</li><li>%nextPage%</li><li>%end%</li><li>%downPage%</li>".$gopagenu."</ul>");
		$this->page = $Page->show();
		$sql = "SELECT a.order_num,a.item_id,a.points,a.fee,a.refund_id,b.name,c.name AS ename FROM box_user_order_detail a LEFT JOIN box_user b on a.openid = b.openid LEFT JOIN box_user c ON a.item_id = c.id WHERE (a.substime>='{$getLastMonth['substime']}' AND a.subetime>='{$getLastMonth['subetime']}') AND a.order_num IN (SELECT order_num FROM box_user_order WHERE paytype=2 AND status=2) AND a.item_id NOT IN (SELECT user_id FROM box_expert_pub_record WHERE pubnums=4 AND pubdate BETWEEN  '{$getLastMonthDate['substime']}' AND  '{$getLastMonthDate['subetime']}') AND a.returntime NOT BETWEEN '{$getThisMonth['substime']}' AND '{$getThisMonth['subetime']}' LIMIT {$Page->firstRow},{$Page->listRows};";//item_id 为医生的ID
		//echo $sql;
		$expert = M()->query($sql);
		foreach ($expert as $key => $value) {
			$user = M('User')->field('level')->where("id={$value['item_id']}")->find();
			$subprice = M('UserLevel')->where("id={$user['level']}")->getField('subprice');

			$condition['pubdate']=array('between',"{$getLastMonthDate['substime']}","{$getLastMonthDate['subetime']}");
			$condition['user_id']=$value['item_id'];
			$pubnums = M('ExpertPubRecord')->where($condition)->getField('pubnums');
			if(!$pubnums){//需要退费的病例数
				$expert[$key]['pubnums']=4;
			}else{
				$expert[$key]['pubnums']=4 - $pubnums;
			}

			$expert[$key]['returnfee']=$subprice * $expert[$key]['pubnums'];//退费的金额
		}
		$this->currentpage = $Page->firstRow + 1; 
		$this->list = $expert;
		$this->display();

	}

	public function returnfee(){
		$condition['order_num']=I('order_num');
		//$data['refund_id']=I('refund_id');
		$data['status']=3;//已退
		$save = M('UserOrderDetail')->where($condition)->save($data);
		$datas = M('UserOrderDetail')->where($condition)->find();
		if($save){//必须有影响的行数
			$datas['refund_id']=I('refund_id');
			M('UserOrderDetail')->add($datas);
			$json['status']=1;		
		}else{
			$json['status']=1;
		}

		$this->ajaxReturn($json,"JSON");
	}

	private function userpoints($type,$openid,$add,$points=0,$is_frozen=1,$order_num=0){
	        $data['openid']=$openid;
	        if(!$points){
	            $data['points'] = M('UserPointsRule')->where("id={$type}")->getField('points');//获得金豆规则对应的金豆 
	        }else{
	            $data['points'] = $points; 
	        }
	        $data['is_frozen']=$is_frozen;
	        $data['order_num']=$order_num;
	        $data['type']=$type;
	        $points = M('UserPoints')->add($data);//插入金豆详细
	        //echo M('UserPoints')->getLastSql();
	        if(($add==1) && ($is_frozen==1)){//金豆不冻结
	            M('User')->where("openid='{$openid}'")->setInc('points',$data['points']);//更新用户全局金豆
	            //echo M('User')->getLastSql();
	            return 1;
	        }elseif(($add!=1)){
	            $isDec = M('User')->where("openid='{$openid}' AND points > 0")->setDec('points',$data['points']);
	            return 1;
	        }
	}
	private function getLastMonth(){
		$d = $c = new DateTime();
		$d->modify( 'first day of last month' );
		$stime = $d->format( 'Y-m-d' );

		$c->modify( "first day of next month" );
		$etime = $c->format( 'Y-m-d' );

		return array("substime"=>strtotime($stime),"subetime"=>strtotime($etime)-1);   
	}

	private function getLastMonthDate(){
		$d = $c = new DateTime();
		$d->modify( 'first day of last month' );
		$stime = $d->format( 'Y-m-d' );

		$c->modify( "last day of this month" );
		$etime = $c->format( 'Y-m-d' );

		return array("substime"=>$stime,"subetime"=>$etime);        
	}

    	private function getThisMonth(){
        		$d = $c = new DateTime();
        		$d->modify( 'first day of this month' );
        		$stime = $d->format( 'Y-m-d' );

       		$c->modify( "first day of next month" );
        		$etime = $c->format( 'Y-m-d' );

       		return array("substime"=>strtotime($stime),"subetime"=>strtotime($etime)-1);        
   	}
}